package testcase.app;

import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import api.app.Card;
import common.Host;
import common.TestData;
/**
 * 实现帮忙医APP获取已绑定体检卡接口\获取体检卡信息接口\手动绑定体检卡接口\体检预约提交用户信息接口测试用例
 * @author laiyn
 *
 */
public class TestCard 
{

	@DataProvider (name = "Test_GetBindingCard")
	/**
	 * 实现帮忙医APP获取已绑定体检卡接口测试环境数据驱动
	 * @return 测试数据
	 * @throws Exception
	 */
	public static String[][] testCardDataProvider_GetBindingCard() throws Exception
	{
		Host.setup("Test");
		TestData td = new TestData("app/TestCard.xml");
		return td.getParam("GetBindingCard");	
	}

	/**
	 * 实现帮忙医APP获取体检卡信息接口测试环境数据驱动
	 * @return 测试数据
	 * @throws Exception
	 */
	@DataProvider (name = "Test_GetCardInfo")
	public static String[][] testCardDataProvider_GetCardInfo() throws Exception
	{
		Host.setup("Test");
		TestData td = new TestData("app/TestCard.xml");
		return td.getParam("GetCardInfo");	
	}
	/**
	 * 实现帮忙医APP手动绑定体检卡接口测试环境数据驱动
	 * @return 测试数据
	 * @throws Exception
	 */
	@DataProvider (name = "Test_BindCard")
	public static String[][] testCardDataProvider_BindCard() throws Exception
	{
		Host.setup("Test");
		TestData td = new TestData("app/TestCard.xml");
		return td.getParam("BindCard");	
	}
	/**
	 * 实现帮忙医APP体检预约提交用户信息接口测试环境数据驱动
	 * @return 测试数据
	 * @throws Exception
	 */
	@DataProvider (name = "Test_SendUserInfo")
	public static String[][] testCardDataProvider_SendUserInfo() throws Exception
	{
		Host.setup("Test");
		TestData td = new TestData("app/TestCard.xml");
		return td.getParam("SendUserInfo");	
	}
	
	@DataProvider (name = "Prod_GetCardInfo")
	/**
	 * 实现帮忙医APP获取体检卡信息接口生产环境数据驱动
	 * @return 测试数据
	 * @throws Exception
	 */
	public static String[][] ProdCardDataProvider_GetCardInfo() throws Exception
	{
		Host.setup("Prod");
		TestData td = new TestData("app/TestCard.xml");
		return td.getParam("GetCardInfo");	
	}

	/**
	 * 实现帮忙医APP体检预约提交用户信息接口生产环境数据驱动
	 * @return 测试数据
	 * @throws Exception
	 */
	@DataProvider (name = "Prod_SendUserInfo")
	public static String[][] ProdCardDataProvider_SendUserInfo() throws Exception
	{
		Host.setup("Prod");
		TestData td = new TestData("app/TestCard.xml");
		return td.getParam("SendUserInfo");	
	}	
	
	@Test (dataProvider = "Test_GetBindingCard", groups = {"Test"})
	/**
	 * 实现帮忙医APP获取已绑定体检卡接口测试环境测试用例
	 * @param caseName -用例名称
	 * @param token
	 * @throws Exception
	 */
	public void testCardTestCase_GetBindingCard(String caseName, String token) throws Exception
	{

		//调用接口并将接口返回数据转换成json格式
		
		String result = Card.get(token);	
		JSONObject jsonResult;
		if (result==null || result.isEmpty())
		{
			jsonResult = null;
		}
		else
		{
			jsonResult = new JSONObject(result);
		}
		//设置检查点
		switch (caseName)
		{
		case "Success":
			Assert.assertEquals(jsonResult.getString("Data").isEmpty(), false);	//接口返回Data字段是否为空	
			break;
		case "Notoken":
			Assert.assertNull(jsonResult); // 接口返回数据是否为空	
			break;
		}
	}
		
		/**
		 * 实现帮忙医APP获取体检卡信息接口测试环境测试用例
		 * @param caseName -用例名称
		 * @param cardNo -卡号
		 * @param token
		 * @throws Exception
		 */
		@Test (dataProvider = "Test_GetCardInfo", groups = {"Test"})
		public void testCardTestCase_GetCardInfo(String caseName, String cardNo, String token) throws Exception
		{

			//调用接口并将接口返回数据转换成json格式
			
			String result = Card.get(cardNo,token);	
			JSONObject jsonResult;
			if (result==null || result.isEmpty())
			{
				jsonResult = null;
			}
			else
			{
				jsonResult = new JSONObject(result);
			}
			//设置检查点
			switch (caseName)
			{
			case "Success":
				// 检查点：接口返回Checkcardno是否与cardNo一致
				String Checkcardno = jsonResult.getJSONObject("Data").getString("Checkcardno");
				Assert.assertEquals(Checkcardno, cardNo);
				break;
			case "CardNoError":
				// 检查点：接口返回Data是否为null
				String checkpoint = jsonResult.getString("Data");
				Assert.assertEquals(checkpoint,"null");	
				break;
			case "Notoken":
				Assert.assertNull(jsonResult); // 接口返回数据是否为空	
				break;				
			}
		}
		/**
		 * 实现帮忙医APP获取体检卡信息接口生产环境测试用例
		 * @param caseName -用例名称
		 * @param cardNo -卡号
		 * @param token
		 * @throws Exception
		 */
		@Test (dataProvider = "Prod_GetCardInfo", groups= {"Prod"})
		public void ProdCardTestCase_GetCardInfo(String caseName, String cardNo, String token) throws Exception
		{

			//调用接口并将接口返回数据转换成json格式
			
			String result = Card.get(cardNo,token);	
			JSONObject jsonResult;
			if (result==null || result.isEmpty())
			{
				jsonResult = null;
			}
			else
			{
				jsonResult = new JSONObject(result);
			}
			//设置检查点
			switch (caseName)
			{
			case "Success":
				// 检查点：接口返回Checkcardno是否与cardNo一致
				String Checkcardno = jsonResult.getJSONObject("Data").getString("Checkcardno");
				Assert.assertEquals(Checkcardno, cardNo);
				break;
			case "CardNoError":
				// 检查点：接口返回Data是否为null
				String checkpoint = jsonResult.getString("Data");
				Assert.assertEquals(checkpoint,"null");	
				break;
			case "Notoken":
				Assert.assertNull(jsonResult); // 接口返回数据是否为空	
				break;				
			}
		}
		/**
		 * 实现帮忙医APP手动绑定体检卡接口测试环境测试用例
		 * @param caseName  -用例名称
		 * @param cardNo  -卡号
		 * @param password  -密码
		 * @param token  
		 * @throws Exception
		 */
		@Test (dataProvider = "Test_BindCard", groups = {"Test"})
		public void testCardTestCase_BindCard(String caseName, String cardNo, String password, String token) throws Exception
		{

			//调用接口并将接口返回数据转换成json格式
			
			String result = Card.put(cardNo,password,token);	
			JSONObject jsonResult;
			if (result==null || result.isEmpty())
			{
				jsonResult = null;
			}
			else
			{
				jsonResult = new JSONObject(result);
			}
			//设置检查点
			String CheckMessage;
			switch (caseName)
			{
			case "Success":
				// 检查点：接口返回valuesField是否与cardNo一致
				String Checkcardno = jsonResult.getJSONObject("Data").getString("valuesField");
				Assert.assertEquals(Checkcardno, cardNo);
				break;
			case "CardNoError":
				CheckMessage = jsonResult.getJSONObject("Data").getString("messageField");
				Assert.assertEquals(CheckMessage, "没有找到匹配的体检卡或密码错误");	//接口返回messageField字段是否为"没有找到匹配的体检卡或密码错误"
				break;
			case "PasswordError":
				CheckMessage = jsonResult.getJSONObject("Data").getString("messageField");
				Assert.assertEquals(CheckMessage, "没有找到匹配的体检卡或密码错误");	//接口返回messageField字段是否为"没有找到匹配的体检卡或密码错误"
				break;
			case "Notoken":
				Assert.assertNull(jsonResult); // 接口返回数据是否为空	
				break;		
						
			}
			
		}
		/**
		 * 实现帮忙医APP体检预约提交用户信息接口测试环境测试用例
		 * @param caseName -用例名称
		 * @param name -用户姓名
		 * @param isMarry -是否结婚
		 * @param certNo -身份证
		 * @param mobile -手机号
		 * @param email -邮件地址
		 * @param token 
		 * @throws Exception
		 */
		@Test (dataProvider = "Test_SendUserInfo", groups = {"Test"})
		public void testCardTestCase_SendUserInfo(String caseName, String name, String isMarry,String certNo, String mobile, String email, String token) throws Exception
		{

			//调用接口并将接口返回数据转换成json格式
			
			String result = Card.post(name, isMarry, certNo, mobile, email, token);	
			JSONObject jsonResult;
			if (result==null || result.isEmpty())
			{
				jsonResult = null;
			}
			else
			{
				jsonResult = new JSONObject(result);
			}
			String Checkpoint;
			switch (caseName)
			{
			case "Success":
				Checkpoint = jsonResult.getJSONObject("Data").getString("success");
				Assert.assertEquals(Checkpoint, "1");	//接口返回success字段是否为1
				break;
				case "NoName":
				Checkpoint = jsonResult.getJSONObject("Data").getString("success");
				Assert.assertEquals(Checkpoint, "1");	//接口返回success字段是否为1
				break;
			case "NoIsMarry":
				Checkpoint = jsonResult.getJSONObject("Data").getString("success");
				Assert.assertEquals(Checkpoint, "1");	//接口返回success字段是否为1
				break;
			case "NoMobile":
				Checkpoint = jsonResult.getJSONObject("Data").getString("success");
				Assert.assertEquals(Checkpoint, "1");	//接口返回success字段是否为1
				break;
			case "NoEmail":
				Checkpoint = jsonResult.getJSONObject("Data").getString("success");
				Assert.assertEquals(Checkpoint, "1");	//接口返回success字段是否为1
				break; 	
			case "Notoken":
				Assert.assertNull(jsonResult); // 接口返回数据是否为空	
				break;	
			case "NoCertNo":
				Assert.assertNull(jsonResult); // 接口返回数据是否为空	
				break; 	
			case "CertNoError":
				Checkpoint = jsonResult.getJSONObject("Data").getString("description");
				Assert.assertEquals(Checkpoint, "身份证号码格式不正确");	//接口返回description字段是否为身份证号码格式不正确
				break; 	
			}
		}
		
		/**
		 * 实现帮忙医APP体检预约提交用户信息接口生产环境测试用例
		 * @param caseName -用例名称
		 * @param name -用户姓名
		 * @param isMarry -是否结婚
		 * @param certNo -身份证
		 * @param mobile -手机号
		 * @param email -邮件地址
		 * @param token 
		 * @throws Exception
		 */
		@Test (dataProvider = "Prod_SendUserInfo", groups = {"Prod"})
		public void ProdCardTestCase_SendUserInfo(String caseName, String name, String isMarry,String certNo, String mobile, String email, String token) throws Exception
		{

			//调用接口并将接口返回数据转换成json格式
			
			String result = Card.post(name, isMarry, certNo, mobile, email, token);	
			JSONObject jsonResult;
			if (result==null || result.isEmpty())
			{
				jsonResult = null;
			}
			else
			{
				jsonResult = new JSONObject(result);
			}
			String Checkpoint;
			switch (caseName)
			{
			case "Success":
				Checkpoint = jsonResult.getJSONObject("Data").getString("success");
				Assert.assertEquals(Checkpoint, "1");	//接口返回success字段是否为1
				break;
				case "NoName":
				Checkpoint = jsonResult.getJSONObject("Data").getString("success");
				Assert.assertEquals(Checkpoint, "1");	//接口返回success字段是否为1
				break;
			case "NoIsMarry":
				Checkpoint = jsonResult.getJSONObject("Data").getString("success");
				Assert.assertEquals(Checkpoint, "1");	//接口返回success字段是否为1
				break;
			case "NoMobile":
				Checkpoint = jsonResult.getJSONObject("Data").getString("success");
				Assert.assertEquals(Checkpoint, "1");	//接口返回success字段是否为1
				break;
			case "NoEmail":
				Checkpoint = jsonResult.getJSONObject("Data").getString("success");
				Assert.assertEquals(Checkpoint, "1");	//接口返回success字段是否为1
				break; 	
			case "Notoken":
				Assert.assertNull(jsonResult); // 接口返回数据是否为空	
				break;	
			case "NoCertNo":
				Assert.assertNull(jsonResult); // 接口返回数据是否为空	
				break; 	
			case "CertNoError":
				Checkpoint = jsonResult.getJSONObject("Data").getString("description");
				Assert.assertEquals(Checkpoint, "身份证号码格式不正确");	//接口返回description字段是否为身份证号码格式不正确
				break; 	
			}
		}		
}
		
